package com.jscloud.bigdata.zk.common;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.CuratorCache;
import org.apache.curator.framework.recipes.cache.CuratorCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class ZookeeperAPIForWatch {
        public static void main(String[] args) throws InterruptedException {
                ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3, Integer.MAX_VALUE);

                //第一步：需要去连接上zk
                CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("192.168.1.110:2181", exponentialBackoffRetry);

                curatorFramework.start();

                //第二步 进行操作

                //创建一些节点
     /*   curatorFramework.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .forPath("/test02/node01","123".getBytes(StandardCharsets.UTF_8));*/

      /*  curatorFramework.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                .forPath("/test02/node01","123".getBytes(StandardCharsets.UTF_8));*/

                //    curatorFramework.delete().forPath("/test02/node01");

   /*     curatorFramework.delete().deletingChildrenIfNeeded()
                .forPath("/test02");*/

                //设置监听机制，监听/test02 节点
                CuratorCache curatorCache = CuratorCache.build(curatorFramework, "/test02");

                //增加监听
                curatorCache.listenable().addListener(new CuratorCacheListener() {
                        @Override
                        public void event(Type type, ChildData oldData, ChildData data) {
                                switch (type){
                                        case NODE_CHANGED: //节点变更
                                                String node_name = data.getPath().replace("/test02", "");
                                                System.out.println("变更节点名称为" + node_name);
                                                System.out.println("变更节点数据为" + new String(data.getData()));

                                                break;
                                        case NODE_CREATED: //节点创建
                                                String node_name2 = data.getPath().replace("/test02", "");
                                                System.out.println("变更节点名称为" + node_name2);
                                                System.out.println("变更节点数据为" + new String(data.getData()));

                                                break;
                                        case NODE_DELETED: //节点删除
                                                String node_name3 = data.getPath().replace("/test02", "");
                                                System.out.println("变更节点名称为" + node_name3);
                                                System.out.println("变更节点数据为" + new String(data.getData()));

                                                break;
                                        default: //兜底处理【选项】
                                                break;
                                }


                        }
                });

                curatorCache.start();
                Thread.sleep(Integer.MAX_VALUE);

                //关闭zookeeper
                //   curatorFramework.close();
        }
}
